home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / quicktime / quicktime vr / vrspeech / application files / comapplication.c next >
Encoding:
Text File  |  2000-06-23  |  10.2 KB  |  408 lines

  1. //////////
  2. //
  3. //    File:        ComApplication.c
  4. //
  5. //    Contains:    Application-specific code for speech recognition sample.
  6. //
  7. //    Written by:    Tim Monroe
  8. //                Based (heavily!) on the MovieShell code written by Apple DTS.
  9. //
  10. //    Copyright:    © 1994-1998 by Apple Computer, Inc., all rights reserved.
  11. //
  12. //    Change History (most recent first):
  13. //
  14. //       <12>         06/19/98    rtm        removed Windows-specific code; removed Test menu support
  15. //       <11>         10/23/97    rtm        moved InitializeQTVR to InitApplication, TerminateQTVR to StopApplication
  16. //       <10>         10/13/97    rtm        ported VRSpeech code to ComApplication.c
  17. //       <9>         10/13/97    rtm        reworked HandleApplicationMenu to use menu identifiers
  18. //       <8>         09/11/97    rtm        merged MacApplication.c and WinApplication.c into ComApplication.c
  19. //       <7>         08/21/97    rtm        first file for Windows; based on MacApplication.c for Mac sample code
  20. //       <6>         06/04/97    rtm        removed call to QTVRUtils_IsQTVRMovie in InitApplicationWindowObject
  21. //       <5>         02/06/97    rtm        fixed window resizing code
  22. //       <4>         12/05/96    rtm        added hooks into MacFramework.c: StopApplication, InitApplicationWindowObject
  23. //       <3>         12/02/96    rtm        added cursor updating to DoIdle
  24. //       <2>         11/27/96    rtm        conversion to personal coding style; added preliminary QTVR support
  25. //       <1>         12/21/94    khs        first file
  26. //       
  27. //////////
  28.  
  29. // header files
  30. #include "ComApplication.h"
  31.  
  32. // header file for the speech functions
  33. #include "VRSpeech.h"
  34.  
  35. // global variables for Macintosh code
  36. long                    gMaxMilliSecToUse = 0L;        
  37. Boolean                    gQTVRMgrIsPresent = false;                // is the QuickTime VR Manager available?        
  38. long                    gQTVRMgrVersion = 0L;                    // the version of the QuickTime VR Manager    
  39. Boolean                    gHasSpeechRec;                            // is the Speech Recognition Manager available?
  40.  
  41.  
  42. //////////
  43. //
  44. // InitApplication
  45. // Do any application-specific initialization.
  46. //
  47. // The theStartPhase parameter determines which "phase" of application start-up is executed,
  48. // *before* the MDI frame window is created or *after*. This distinction is relevant only on
  49. // Windows, so on MacOS, you should always use kInitAppPhase_BothPhases.
  50. //
  51. //////////
  52.  
  53. void InitApplication (UInt32 theStartPhase)
  54. {
  55.     // ***do any start-up activities that should occur before the MDI frame window is created
  56.     if (theStartPhase & kInitAppPhase_BeforeCreateFrameWindow) {
  57.  
  58.         // make sure that the QuickTime VR Manager is available in the present operating environment;
  59.         // if it is, get its version and (if necessary) initialize it
  60.         if (QTVRUtils_IsQTVRMgrInstalled()) {
  61.             gQTVRMgrIsPresent = true;
  62.             gQTVRMgrVersion = QTVRUtils_GetQTVRVersion();        // get the version of the QuickTime VR Manager
  63.         }
  64.         
  65.         // initialize speech recognition capabilities
  66.         gHasSpeechRec = false;        // assume the worst
  67.         VRSpeech_Init();
  68.     }
  69. }
  70.  
  71.  
  72. //////////
  73. //
  74. // StopApplication
  75. // Do any application-specific shut-down.
  76. //
  77. //////////
  78.  
  79. void StopApplication (UInt32 theStopPhase)
  80. {
  81.     // do any shut-down activities that should occur after the movie windows are destroyed
  82.     if (theStopPhase & kStopAppPhase_AfterDestroyWindows) {    
  83.         // shutdown speech recognition capabilities, if enabled
  84.         if (gHasSpeechRec)
  85.             VRSpeech_Stop();    
  86.     }
  87. }
  88.  
  89.  
  90. //////////
  91. //
  92. // DoIdle
  93. // Do any processing that can/should occur at idle time.
  94. //
  95. //////////
  96.  
  97. void DoIdle (WindowReference theWindow)
  98. {
  99.     WindowObject         myWindowObject = NULL;
  100.     GrafPtr             mySavedPort;
  101.     
  102.     GetPort(&mySavedPort);
  103.     MacSetPort(GetPortFromWindowReference(theWindow));
  104.     
  105.     myWindowObject = GetWindowObjectFromWindow(theWindow);
  106.     if (myWindowObject != NULL) {
  107.         MovieController        myMC = NULL;
  108.     
  109.         myMC = (**myWindowObject).fController;
  110.         if (myMC != NULL) {
  111.             // restore the cursor to the arrow
  112.             // if it's outside the front movie window or outside the window's visible region
  113.             if (theWindow == GetFrontMovieWindow()) {
  114.                 Rect    myRect;
  115.                 Point    myPoint;
  116.                 
  117.                 GetMouse(&myPoint);
  118.                 MCGetControllerBoundsRect(myMC, &myRect);
  119.                 if (!MacPtInRect(myPoint, &myRect) || !PtInRgn(myPoint, GetPortFromWindowReference(theWindow)->visRgn))
  120.                     MacSetCursor(&qd.arrow);
  121.             }
  122.         }
  123.     }
  124.  
  125.     // handle speech-initiated periodics
  126.     if (gHasSpeechRec)
  127.         VRSpeech_DoEventLoopSpinCheck();
  128.  
  129.     MacSetPort(mySavedPort);
  130. }
  131.  
  132.  
  133. //////////
  134. //
  135. // DoUpdateWindow
  136. // Update the specified window.
  137. //
  138. //////////
  139.  
  140. void DoUpdateWindow (WindowReference theWindow, Rect *theRefreshArea)
  141. {
  142. #pragma unused(theRefreshArea)
  143.     GrafPtr             mySavedPort;
  144.     
  145.     GetPort(&mySavedPort);
  146.     MacSetPort(GetPortFromWindowReference(theWindow));
  147.     
  148.     BeginUpdate(theWindow);
  149.     
  150.     // draw the movie controller and its movie
  151.     MCDoAction(GetMCFromWindow(theWindow), mcActionDraw, theWindow);
  152.     
  153.     EndUpdate(theWindow);
  154.     MacSetPort(mySavedPort);
  155. }
  156.  
  157.  
  158. //////////
  159. //
  160. // HandleContentClick
  161. // Handle mouse button clicks in the specified window.
  162. //
  163. //////////
  164.  
  165. void HandleContentClick (WindowReference theWindow, EventRecord *theEvent)
  166. {
  167. #pragma unused(theEvent)
  168.  
  169.     GrafPtr             mySavedPort;
  170.     
  171.     GetPort(&mySavedPort);
  172.     MacSetPort(GetPortFromWindowReference(theWindow));
  173.     
  174.     // @@@INSERT APPLICATION-SPECIFIC CONTENT CLICKING FUNCTIONALITY HERE
  175.  
  176.     MacSetPort(mySavedPort);
  177. }
  178.  
  179.  
  180. //////////
  181. //
  182. // HandleApplicationKeyPress
  183. // Handle application-specific key presses.
  184. // Returns true if the key press was handled, false otherwise.
  185. //
  186. //////////
  187.  
  188. Boolean HandleApplicationKeyPress (char theCharCode)
  189. {
  190.     Boolean        isHandled = true;
  191.     
  192.     switch (theCharCode) {
  193.     
  194.         // @@@HANDLE APPLICATION-SPECIFIC KEY PRESSES HERE
  195.  
  196.         default:
  197.             isHandled = false;
  198.             break;
  199.     }
  200.  
  201.     return(isHandled);
  202. }
  203.  
  204.  
  205. //////////
  206. //
  207. // CreateMovieWindow
  208. // Create a window to display a movie in.
  209. //
  210. //////////
  211.  
  212. WindowRef CreateMovieWindow (Rect *theRect, Str255 theTitle)
  213. {
  214.     WindowRef            myWindow;
  215.         
  216.     myWindow = NewCWindow(NULL, theRect, theTitle, false, noGrowDocProc, (WindowPtr)-1L, true, 0);
  217.     return(myWindow);
  218. }
  219.  
  220.  
  221. //////////
  222. //
  223. // HandleApplicationMenu
  224. // Handle selections in the application's menus.
  225. //
  226. // The theMenuItem parameter is a UInt16 version of the Windows "menu item identifier". 
  227. // When called from Windows, theMenuItem is simply the menu item identifier passed to the window proc.
  228. // When called from MacOS, theMenuItem is constructed like this:
  229. //     *high-order 8 bits == the Macintosh menu ID (1 thru 256)
  230. //     *low-order 8 bits == the Macintosh menu item (sequential from 1 to ordinal of last menu item in menu)
  231. // In this way, we can simplify the menu-handling code. There are, however, some limitations,
  232. // mainly that the menu item identifiers on Windows must be derived from the Mac values. 
  233. //
  234. //////////
  235.  
  236. void HandleApplicationMenu (UInt16 theMenuItem)
  237. {
  238. #pragma unused(theMenuItem)
  239.     // currently, the Mac application has NO app-specific menus; you could change that.
  240. }
  241.  
  242.  
  243. //////////
  244. //
  245. // AdjustApplicationMenus
  246. // Adjust state of items in the application's menus.
  247. //
  248. // Currently, the Mac application has NO app-specific menus; you could change that.
  249. //
  250. //////////
  251.  
  252. void AdjustApplicationMenus (WindowReference theWindow, MenuReference theMenu)
  253. {
  254. #pragma unused(theWindow, theMenu)
  255.     // we don't allow creating new files here...
  256.     SetMenuItemState(GetMenuHandle(mFile), iNew, kDisableMenuItem);
  257. }
  258.  
  259.  
  260. //////////
  261. //
  262. // DoApplicationEventLoopAction
  263. // Perform any application-specific event loop actions.
  264. //
  265. // Return true to indicate that we've completely handled the event here, false otherwise.
  266. //
  267. //////////
  268.  
  269. Boolean DoApplicationEventLoopAction (EventRecord *theEvent)
  270. {
  271. #pragma unused(theEvent)
  272.     return(false);            // no-op for now
  273. }
  274.  
  275.  
  276. //////////
  277. //
  278. // AddControllerFunctionality
  279. // Configure the movie controller.
  280. //
  281. //////////
  282.  
  283. void AddControllerFunctionality (MovieController theMC)
  284. {
  285.     long            myControllerFlags;
  286.     
  287.     // CLUT table use    
  288.     MCDoAction(theMC, mcActionGetFlags, &myControllerFlags);
  289.     MCDoAction(theMC, mcActionSetFlags, (void *)(myControllerFlags | mcFlagsUseWindowPalette));
  290.  
  291.     // enable keyboard event handling    
  292.     MCDoAction(theMC, mcActionSetKeysEnabled, (void *)true);
  293.     
  294.     // disable drag support
  295.     MCDoAction(theMC, mcActionSetDragEnabled, (void *)false);
  296. }
  297.  
  298.  
  299. //////////
  300. //
  301. // InitApplicationWindowObject
  302. // Do any application-specific initialization of the window object.
  303. //
  304. //////////
  305.  
  306. void InitApplicationWindowObject (WindowObject theWindowObject)
  307. {
  308.     Track                    myQTVRTrack = NULL;
  309.     Movie                    myMovie = NULL;
  310.     MovieController            myMC = NULL;
  311.     QTVRInstance            myInstance = NULL;
  312.         
  313.     if (theWindowObject == NULL)
  314.         return;
  315.  
  316.     // make sure we can safely call the QTVR API
  317.     if (!gQTVRMgrIsPresent)
  318.         return;
  319.  
  320.     // find the QTVR track, if there is one
  321.     myMC = (**theWindowObject).fController;
  322.     myMovie = (**theWindowObject).fMovie;
  323.     myQTVRTrack = QTVRGetQTVRTrack(myMovie, 1);
  324.     
  325.     QTVRGetQTVRInstance(&myInstance, myQTVRTrack, myMC);
  326.     (**theWindowObject).fInstance = myInstance;
  327.  
  328.     // do any QTVR window configuration
  329.     if (myInstance != NULL) {
  330.         
  331.         // set unit to radians
  332.         QTVRSetAngularUnits(myInstance, kQTVRRadians);
  333.  
  334.         // install a node entering procedure
  335.         QTVRSetEnteringNodeProc(myInstance, NewQTVREnteringNodeProc(VRSpeech_SpeakNameOfNode), (long)myInstance, 0);
  336.         
  337.         // update
  338.         QTVRUpdate(myInstance, kQTVRCurrentMode);
  339.     }
  340. }
  341.  
  342.  
  343. //////////
  344. //
  345. // RemoveApplicationWindowObject
  346. // Do any application-specific clean-up of the window object.
  347. //
  348. //////////
  349.  
  350. void RemoveApplicationWindowObject (WindowObject theWindowObject)
  351. {
  352.     OSErr                myErr = noErr;
  353.     QTVRInstance        myInstance = NULL;
  354.         
  355.     if (theWindowObject == NULL)
  356.         return;
  357.         
  358.     myInstance = (**theWindowObject).fInstance;
  359.     
  360.     // @@@INSERT APPLICATION-SPECIFIC WINDOW OBJECT CLEAN-UP HERE
  361.  
  362.     // DoDestroyMovieWindow in MacFramework.c or MovieWndProc in WinFramework.c
  363.     // releases the window object itself
  364. }
  365.  
  366.  
  367. //////////
  368. //
  369. // ApplicationMCActionFilterProc 
  370. // Intercept some mc actions for the movie controller.
  371. //
  372. // NOTE: The theRefCon parameter is a handle to a window object record.
  373. //
  374. //////////
  375.  
  376. PASCAL_RTN Boolean ApplicationMCActionFilterProc (MovieController theMC, short theAction, void *theParams, long theRefCon)
  377. {
  378. #pragma unused(theMC, theParams)
  379.  
  380.     Boolean                isHandled = false;
  381.     WindowObject        myWindowObject = NULL;
  382.     
  383.     myWindowObject = (WindowObject)theRefCon;
  384.     if (myWindowObject == NULL)
  385.         return(isHandled);
  386.         
  387.     switch (theAction) {
  388.     
  389.         // handle window resizing
  390.         case mcActionControllerSizeChanged:
  391.             SizeWindowToMovie(myWindowObject);
  392.             break;
  393.  
  394.         // handle idle events
  395.         case mcActionIdle:
  396.             DoIdle((**myWindowObject).fWindow);
  397.             break;
  398.             
  399.         case mcActionMouseDown:
  400.             break;
  401.         
  402.         default:
  403.             break;
  404.             
  405.     }    // switch (theAction)
  406.     
  407.     return(isHandled);    
  408. }